import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info_cancellation_info.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_purchased_subscription_cancellation_info.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_cancellation_survey_result_from_api_one.dart';

abstract class CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne {
  GooglePlayPurchasedSubscriptionInfoCancellationInfo call({
    required GooglePlayApiPurchasedSubscriptionCancellationInfo apiCancellationInfo,
  });
}

class CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOneImpl
    implements CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne {
  const CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOneImpl({
    required CreateGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne
        createGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne,
  }) : _createGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne;

  final CreateGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne;

  @override
  GooglePlayPurchasedSubscriptionInfoCancellationInfo call({
    required GooglePlayApiPurchasedSubscriptionCancellationInfo apiCancellationInfo,
  }) {
    if (apiCancellationInfo is GooglePlayApiPurchasedSubscriptionUserCancellationInfo) {
      final surveyResult = _createGooglePlayPurchasedSubscriptionInfoCancellationSurveyResultFromApiOne(
        apiSurveyResult: apiCancellationInfo.surveyResult,
      );

      return GooglePlayPurchasedSubscriptionInfoUserCancellationInfo(
        time: apiCancellationInfo.time,
        surveyResult: surveyResult,
      );
    }

    if (apiCancellationInfo is GooglePlayApiPurchasedSubscriptionSystemCancellationInfo) {
      return const GooglePlayPurchasedSubscriptionInfoSystemCancellationInfo();
    }

    if (apiCancellationInfo is GooglePlayApiPurchasedSubscriptionDeveloperCancellationInfo) {
      return const GooglePlayPurchasedSubscriptionInfoDeveloperCancellationInfo();
    }

    if (apiCancellationInfo is GooglePlayApiPurchasedSubscriptionNewSubscriptionCancellationInfo) {
      return const GooglePlayPurchasedSubscriptionInfoNewSubscriptionCancellationInfo();
    }

    throw UnimplementedError();
  }
}
